#include <sys/epoll.h>
#include <unistd.h>
#include <stdlib.h>
#include "log.hpp"
class Epoll
{
public:
    Epoll()
    {
    }
    void Epoll_create()
    {
        int n = epoll_create(256);
        if (n < 0)
        {
            logMessage(FATAL, "Epoll_create error, %d:%s\n", errno, strerror(errno));
            exit(4);
        }
        _epollfd = n;
    }
    void AddSockToEpoll(int sock, uint32_t events)
    {
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = sock;
        epoll_ctl(_epollfd, EPOLL_CTL_ADD, sock, &ev);
    }
    int WaitEpoll(struct epoll_event *revs, int revs_num)
    {
        return epoll_wait(_epollfd, revs, revs_num, 5000);
    }
    int CtrlEpoll(int sock, uint32_t events)
    {
        events |= EPOLLET;
        struct epoll_event ev;
        ev.data.fd = sock;
        ev.events = events;
        return epoll_ctl(_epollfd, EPOLL_CTL_MOD, sock, &ev);
    }
    bool DelFromEpoll(int sock)
    {
        int n = epoll_ctl(_epollfd, EPOLL_CTL_DEL, sock, nullptr);
        return n == 0;
    }

    ~Epoll()
    {
    }

private:
    int _epollfd;
};